3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
8 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase
{
10 use MediaWikiCoversValidator
;
12 protected static function expandVariables( $text ) {
13 return strtr( $text, [
14 '{blankVer}' => ResourceLoaderTestCase
::BLANK_VERSION
18 protected static function makeContext( $extraQuery = [] ) {
19 $conf = new HashConfig( [
20 'ResourceLoaderSources' => [],
21 'ResourceModuleSkinStyles' => [],
22 'ResourceModules' => [],
23 'EnableJavaScriptTest' => false,
24 'ResourceLoaderDebug' => false,
25 'LoadScript' => '/w/load.php',
27 return new ResourceLoaderContext(
28 new ResourceLoader( $conf ),
29 new FauxRequest( array_merge( [
33 'target' => 'phpunit',
38 protected static function makeModule( array $options = [] ) {
39 return new ResourceLoaderTestModule( $options );
42 protected static function makeSampleModules() {
45 'test.top' => [ 'position' => 'top' ],
46 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
47 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
48 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
49 'test.shouldembed' => [ 'shouldEmbed' => true ],
51 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
52 'test.styles.mixed' => [],
53 'test.styles.noscript' => [
54 'type' => ResourceLoaderModule
::LOAD_STYLES
,
55 'group' => 'noscript',
57 'test.styles.user' => [
58 'type' => ResourceLoaderModule
::LOAD_STYLES
,
61 'test.styles.user.empty' => [
62 'type' => ResourceLoaderModule
::LOAD_STYLES
,
64 'isKnownEmpty' => true,
66 'test.styles.private' => [
67 'type' => ResourceLoaderModule
::LOAD_STYLES
,
69 'styles' => '.private{}',
71 'test.styles.shouldembed' => [
72 'type' => ResourceLoaderModule
::LOAD_STYLES
,
73 'shouldEmbed' => true,
74 'styles' => '.shouldembed{}',
78 'test.scripts.top' => [ 'position' => 'top' ],
79 'test.scripts.user' => [ 'group' => 'user' ],
80 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
81 'test.scripts.raw' => [ 'isRaw' => true ],
82 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
84 'test.ordering.a' => [ 'shouldEmbed' => false ],
85 'test.ordering.b' => [ 'shouldEmbed' => false ],
86 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
87 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
88 'test.ordering.e' => [ 'shouldEmbed' => false ],
90 return array_map( function ( $options ) {
91 return self
::makeModule( $options );
96 * @covers ResourceLoaderClientHtml::getDocumentAttributes
98 public function testGetDocumentAttributes() {
99 $client = new ResourceLoaderClientHtml( self
::makeContext() );
100 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
104 * @covers ResourceLoaderClientHtml::__construct
105 * @covers ResourceLoaderClientHtml::setModules
106 * @covers ResourceLoaderClientHtml::setModuleStyles
107 * @covers ResourceLoaderClientHtml::setModuleScripts
108 * @covers ResourceLoaderClientHtml::getData
109 * @covers ResourceLoaderClientHtml::getContext
111 public function testGetData() {
112 $context = self
::makeContext();
113 $context->getResourceLoader()->register( self
::makeSampleModules() );
115 $client = new ResourceLoaderClientHtml( $context );
116 $client->setModules( [
118 'test.private.bottom',
121 'test.shouldembed.empty',
125 $client->setModuleStyles( [
127 'test.styles.user.empty',
128 'test.styles.private',
130 'test.styles.shouldembed',
131 'test.unregistered.styles',
133 $client->setModuleScripts( [
135 'test.scripts.user.empty',
137 'test.scripts.shouldembed',
138 'test.unregistered.scripts',
143 'test.private.top' => 'loading',
144 'test.private.bottom' => 'loading',
145 'test.shouldembed.empty' => 'ready',
146 'test.shouldembed' => 'loading',
147 'test.styles.pure' => 'ready',
148 'test.styles.user.empty' => 'ready',
149 'test.styles.private' => 'ready',
150 'test.styles.shouldembed' => 'ready',
151 'test.scripts' => 'loading',
152 'test.scripts.top' => 'loading',
153 'test.scripts.user.empty' => 'ready',
154 'test.scripts.shouldembed' => 'loading',
166 'test.scripts.shouldembed',
169 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
171 'test.private.bottom',
178 $access = TestingAccessWrapper
::newFromObject( $client );
179 $this->assertEquals( $expected, $access->getData() );
183 * @covers ResourceLoaderClientHtml::setConfig
184 * @covers ResourceLoaderClientHtml::setExemptStates
185 * @covers ResourceLoaderClientHtml::getHeadHtml
186 * @covers ResourceLoaderClientHtml::getLoad
187 * @covers ResourceLoader::makeLoaderStateScript
189 public function testGetHeadHtml() {
190 $context = self
::makeContext();
191 $context->getResourceLoader()->register( self
::makeSampleModules() );
193 $client = new ResourceLoaderClientHtml( $context );
194 $client->setConfig( [ 'key' => 'value' ] );
195 $client->setModules( [
199 $client->setModuleStyles( [
201 'test.styles.private',
203 $client->setModuleScripts( [
206 $client->setExemptStates( [
207 'test.exempt' => 'ready',
210 // @codingStandardsIgnoreStart Generic.Files.LineLength
211 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
212 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
213 . 'mw.config.set({"key":"value"});'
214 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
215 . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
216 . 'mw.loader.load(["test.top"]);'
217 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
218 . '});</script>' . "\n"
219 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
220 . '<style>.private{}</style>' . "\n"
221 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
222 // @codingStandardsIgnoreEnd
223 $expected = self
::expandVariables( $expected );
225 $this->assertEquals( $expected, $client->getHeadHtml() );
229 * @covers ResourceLoaderClientHtml::getBodyHtml
230 * @covers ResourceLoaderClientHtml::getLoad
232 public function testGetBodyHtml() {
233 $context = self
::makeContext();
234 $context->getResourceLoader()->register( self
::makeSampleModules() );
236 $client = new ResourceLoaderClientHtml( $context );
237 $client->setConfig( [ 'key' => 'value' ] );
238 $client->setModules( [
240 'test.private.bottom',
242 $client->setModuleScripts( [
247 $expected = self
::expandVariables( $expected );
249 $this->assertEquals( $expected, $client->getBodyHtml() );
252 public static function provideMakeLoad() {
254 // @codingStandardsIgnoreStart Generic.Files.LineLength
257 'modules' => [ 'test.unknown' ],
258 'only' => ResourceLoaderModule
::TYPE_STYLES
,
263 'modules' => [ 'test.styles.private' ],
264 'only' => ResourceLoaderModule
::TYPE_STYLES
,
265 'output' => '<style>.private{}</style>',
269 'modules' => [ 'test.private.top' ],
270 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
271 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
275 // Eg. startup module
276 'modules' => [ 'test.scripts.raw' ],
277 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
278 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
281 'context' => [ 'sync' => true ],
282 'modules' => [ 'test.scripts.raw' ],
283 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
284 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
288 'modules' => [ 'test.scripts.user' ],
289 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
290 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
293 'context' => [ 'debug' => true ],
294 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
295 'only' => ResourceLoaderModule
::TYPE_STYLES
,
296 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
297 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
300 'context' => [ 'debug' => false ],
301 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
302 'only' => ResourceLoaderModule
::TYPE_STYLES
,
303 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
307 'modules' => [ 'test.styles.noscript' ],
308 'only' => ResourceLoaderModule
::TYPE_STYLES
,
309 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
313 'modules' => [ 'test.shouldembed' ],
314 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
315 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
319 'modules' => [ 'test.styles.shouldembed' ],
320 'only' => ResourceLoaderModule
::TYPE_STYLES
,
321 'output' => '<style>.shouldembed{}</style>',
325 'modules' => [ 'test.scripts.shouldembed' ],
326 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
327 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
331 'modules' => [ 'test', 'test.shouldembed' ],
332 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
333 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
337 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
338 'only' => ResourceLoaderModule
::TYPE_STYLES
,
340 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
341 . '<style>.shouldembed{}</style>'
345 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
346 'only' => ResourceLoaderModule
::TYPE_STYLES
,
348 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
349 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
350 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
352 // @codingStandardsIgnoreEnd
357 * @dataProvider provideMakeLoad
358 * @covers ResourceLoaderClientHtml::makeLoad
359 * @covers ResourceLoaderClientHtml::makeContext
360 * @covers ResourceLoader::makeModuleResponse
361 * @covers ResourceLoaderModule::getModuleContent
362 * @covers ResourceLoader::getCombinedVersion
363 * @covers ResourceLoader::createLoaderURL
364 * @covers ResourceLoader::createLoaderQuery
365 * @covers ResourceLoader::makeLoaderQuery
366 * @covers ResourceLoader::makeInlineScript
368 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
369 $context = self
::makeContext( $extraQuery );
370 $context->getResourceLoader()->register( self
::makeSampleModules() );
371 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery );
372 $expected = self
::expandVariables( $expected );
373 $this->assertEquals( $expected, (string)$actual );